package com.witframework.web.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class AjaxFormTag extends BodyTagSupport
{
  private static final String AJAXFORMTAG = "jxfrmtgcj2006";
  private String method = "GET";
  private String id = null;
  private String name = null;
  private String action = null;
  private String beforeAction = null;
  private String afterAction = null;
  private String error = null;
  private String handler = null;
  private String urlFunction = null;
  private String onSubmit = null;
  private String sBody = null;

  public String getMethod()
  {
    return this.method;
  }

  public void setMethod(String paramString)
  {
    this.method = paramString;
  }

  public void setId(String paramString)
  {
    this.id = paramString;
  }

  public String getId()
  {
    return this.id;
  }

  public void setUrlFunction(String paramString)
  {
    this.urlFunction = paramString;
  }

  public String getUrlFunction()
  {
    return this.urlFunction;
  }

  public void setError(String paramString)
  {
    this.error = paramString;
  }

  public String getError()
  {
    return this.error;
  }

  public void setName(String paramString)
  {
    this.name = paramString;
  }

  public String getName()
  {
    return this.name;
  }

  public void setAction(String paramString)
  {
    this.action = paramString;
  }

  public String getAction()
  {
    return this.action;
  }

  public void setHandler(String paramString)
  {
    this.handler = paramString;
  }

  public String getHandler()
  {
    return this.handler;
  }

  public void setOnSubmit(String paramString)
  {
    this.onSubmit = paramString;
  }

  public String getOnSubmit()
  {
    return this.onSubmit;
  }

  public void setBeforeAction(String paramString)
  {
    this.beforeAction = paramString;
  }

  public String getBeforeAction()
  {
    return this.beforeAction;
  }

  public void setAfterAction(String paramString)
  {
    this.afterAction = paramString;
  }

  public String getAfterAction()
  {
    return this.afterAction;
  }

  public int doAfterBody()    throws JspException  {
    BodyContent localBodyContent = getBodyContent();
    if (localBodyContent == null)
    {
      this.sBody = "";
    }
    else
    {
      this.sBody = localBodyContent.getString();
      localBodyContent.clearBody();
    }
    this.sBody = this.sBody.trim();
    return 0;
  }

  public int doEndTag()    throws JspException  {
    Object localObject;
    StringBuffer localStringBuffer = new StringBuffer("");
    if ((this.id == null) && (this.name == null))
    {
      localObject = (Integer)this.pageContext.getAttribute("jxfrmtgcj2006", 1);
      if (localObject == null)
        localObject = new Integer(0);
      this.id = "jxfrmtgcj2006_" + localObject;
      this.pageContext.setAttribute("jxfrmtgcj2006", new Integer(((Integer)localObject).intValue() + 1), 1);
    }
    localStringBuffer.append("<form");
    if (this.id != null)
    {
      localStringBuffer.append(" id=\"");
      localStringBuffer.append(this.id);
      localStringBuffer.append("\"");
    }
    if (this.name != null)
    {
      localStringBuffer.append(" name=\"");
      localStringBuffer.append(this.name);
      localStringBuffer.append("\"");
    }
    if (this.method != null)
    {
      localStringBuffer.append(" method=\"");
      localStringBuffer.append(this.method);
      localStringBuffer.append("\"");
    }
    if (this.action != null)
    {
      localStringBuffer.append(" action=\"");
      localStringBuffer.append(this.action);
      localStringBuffer.append("\"");
    }
    localStringBuffer.append(" onSubmit=\"");
    if (this.onSubmit != null)
    {
      localStringBuffer.append(" if (");
      localStringBuffer.append(this.onSubmit);
      localStringBuffer.append("(){");
    }
    localStringBuffer.append("cjAjaxFormParam=formParametersBy");
    if (this.id != null)
    {
      localStringBuffer.append("Id('");
      localStringBuffer.append(this.id);
    }
    else
    {
      localStringBuffer.append("Name('");
      localStringBuffer.append(this.name);
    }
    localStringBuffer.append("');cjAjaxEngine");
    if ("GET".equalsIgnoreCase(this.method))
      localStringBuffer.append("Get(");
    else
      localStringBuffer.append("Post(");
    if (this.action == null)
    {
      localObject = (HttpServletRequest)this.pageContext.getRequest();
      String str1 = ((HttpServletRequest)localObject).getRequestURI();
      String str2 = ((HttpServletRequest)localObject).getQueryString();
      if ((str2 != null) && (str2.length() > 0))
        str1 = str1 + "?" + str2;
      if ("GET".equalsIgnoreCase(this.method))
      {
        localStringBuffer.append("'");
        localStringBuffer.append(str1);
        localStringBuffer.append("'");
        if (str1.indexOf("?") > 0)
          localStringBuffer.append("+'&'+");
        else
          localStringBuffer.append("+'?'+");
        localStringBuffer.append("cjAjaxFormParam");
      }
      else
      {
        localStringBuffer.append("'" + str1 + "'");
      }
    }
    else if ("GET".equalsIgnoreCase(this.method))
    {
      localStringBuffer.append("'");
      localStringBuffer.append(this.action);
      localStringBuffer.append("'");
      if (this.action.indexOf("?") > 0)
        localStringBuffer.append("+'&'+");
      else
        localStringBuffer.append("+'?'+");
      localStringBuffer.append("cjAjaxFormParam");
    }
    else
    {
      localStringBuffer.append("'" + this.action + "'");
    }
    localStringBuffer.append(",");
    if (!("GET".equalsIgnoreCase(this.method)))
    {
      localStringBuffer.append("cjAjaxFormParam");
      localStringBuffer.append(",");
    }
    if (this.handler == null)
      localStringBuffer.append("null");
    else
      localStringBuffer.append(this.handler);
    localStringBuffer.append(",");
    if (this.error == null)
      localStringBuffer.append("null");
    else
      localStringBuffer.append(this.error);
    localStringBuffer.append(",");
    if (this.beforeAction == null)
      localStringBuffer.append("null");
    else
      localStringBuffer.append(this.beforeAction);
    localStringBuffer.append(",");
    if (this.afterAction == null)
      localStringBuffer.append("null");
    else
      localStringBuffer.append(this.afterAction);
    localStringBuffer.append(");");
    if (this.onSubmit != null)
      localStringBuffer.append("}");
    localStringBuffer.append("return false;\">");
    localStringBuffer.append(this.sBody);
    localStringBuffer.append("</form>");
    try
    {
      this.pageContext.getOut().write(localStringBuffer.toString());
    }
    catch (Exception localException)
    {
      throw new JspException("AjaxForm: could not write data");
    }
    dropData();
    return 6;
  }

  public void release()  {
    dropData();
  }

  private void dropData()  {
    this.method = "GET";
    this.id = null;
    this.name = null;
    this.action = null;
    this.beforeAction = null;
    this.afterAction = null;
    this.error = null;
    this.handler = null;
    this.urlFunction = null;
    this.onSubmit = null;
    this.sBody = null;
  }
}
